package com.badou.validate.web;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.stereotype.Controller;

import com.badou.core.base.support.struts.BaseStrutsAction;
import com.badou.core.base.support.struts.JsonReturnBean;
import com.badou.tools.common.util.ParamUtils;
import com.badou.tools.common.util.StringUtils;
import com.badou.tools.vendors.struts2.annotation.result.JSONResult;

@Controller
public class CheckCodeAction extends BaseStrutsAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected JsonReturnBean returnBean;
	
	@JSON(name="rbean")
	public JsonReturnBean getReturnBean() {
		return returnBean;
	}
	@Action(interceptorRefs = @InterceptorRef("baseJsonStack"))
	@JSONResult(params={"root", "returnBean"})
	public String checkCode(){
		try{
			returnBean = new JsonReturnBean();
			String sCodes = null;
			try {
				sCodes = request.getSession().getAttribute("codes").toString().toLowerCase();
			} catch (Exception e) {
				logger.error(e);
			}
			String codes = ParamUtils.getParameter(request,"codes").toLowerCase();
			if(StringUtils.isNotBlank(codes) && StringUtils.isNotBlank(sCodes) && codes.equals(sCodes)){
				//request.getSession().invalidate();
				returnBean.setHasOk(true);
				returnBean.setTip(JsonReturnBean.TIP_SUCCESS);
				returnBean.setMessage("验证成功！");
			}else{
				returnBean.setHasOk(false);
				returnBean.setTip(JsonReturnBean.TIP_FAIL);
				returnBean.setMessage("验证码有误，请刷新与重新输入");
			}
		}catch(Exception e){
			logger.error(e);
			returnBean.setHasOk(false);
			returnBean.setTip(JsonReturnBean.TIP_FAIL);
			returnBean.setMessage("验证码有误，请刷新与输入");
		}
		return "checkCode";
	}
}
